package com.example.sunmiv1.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.ChooseCouponAdapter;
import com.example.sunmiv1.base.BaseActivity;
import com.example.sunmiv1.entity.GoodBean;
import com.example.sunmiv1.entity.VipListBean;
import com.example.sunmiv1.utils.MathUtil;
import com.example.sunmiv1.utils.net.ACache;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChooseCouponActivity extends BaseActivity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.chooseCoupon_listView)
    ListView chooseCouponListView;

    private double allPrice;
    private ACache aCache;
    private ChooseCouponAdapter chooseCouponAdapter;
    private List<VipListBean.InfoBean.CouponListBean> couponList;
    private int select = 9999;
    private List<GoodBean.DataBean.GoodsBean> chooseList;
    private VipListBean.InfoBean infoBean;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_choose_coupon);
        ButterKnife.bind(this);
        aCache = ACache.get(context);
        titleTitle.setText(R.string.chooseCoupon);
        allPrice = getIntent().getDoubleExtra("allMoney", 0);
        couponList = (List<VipListBean.InfoBean.CouponListBean>) getIntent().getSerializableExtra("couponList");
        chooseList = (List<GoodBean.DataBean.GoodsBean>) getIntent().getSerializableExtra("chooseList");
        infoBean = (VipListBean.InfoBean) getIntent().getSerializableExtra("infoBean");
        setChooseCouponAdapter();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        chooseCouponListView.setOnItemClickListener((parent, view, position, id) -> {
            if (select == 9999) {
                /*if (allPrice < Double.parseDouble(couponList.get(position).getNeed_money())) {
                    Toast.makeText(this, "不滿足優惠券條件", Toast.LENGTH_SHORT).show();
                    return;
                }*/
                /**所有商品都可用*/
                if (checkCoupon(position)) {
                    return;
                }
                select = position;
            } else {
                if (position == select) {
                    select = 9999;
                } else {
                    if (checkCoupon(position)) {
                        return;
                    }
                    select = position;
                }
            }
            chooseCouponAdapter.setSelect(select);
            setChooseCouponAdapter();
        });
    }

    private boolean checkCoupon(int position) {
        /**所有商品都可用*/
        if ("1".equals(couponList.get(position).getGoods_type()) ||
                TextUtils.isEmpty(couponList.get(position).getGoods_type())) {
            if (Double.parseDouble(couponList.get(position).getNeed_money()) > allPrice) {
                Toast.makeText(context, "不滿足該優惠券的條件", Toast.LENGTH_SHORT).show();
                return true;
            }

        } else if ("2".equals(couponList.get(position).getGoods_type())) {//部分商品可用
            if (Double.parseDouble(couponList.get(position).getNeed_money()) >
                    getCouponGoodsPrice(2, couponList.get(position))) {
                Toast.makeText(context, "不滿足該優惠券的條件", Toast.LENGTH_SHORT).show();
                return true;
            }
        } else if ("3".equals(couponList.get(position).getGoods_type())) {
            if (Double.parseDouble(couponList.get(position).getNeed_money()) >
                    getCouponGoodsPrice(3, couponList.get(position))) {
                Toast.makeText(context, "不滿足該優惠券的條件", Toast.LENGTH_SHORT).show();
                return true;
            }
        }
        if (Double.parseDouble(couponList.get(position).getReduce_money()) > allPrice) {
            Toast.makeText(context, "優惠券減免金額大於總金額", Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }

    /**
     * 用來計算商品的折扣和總價格
     */
    private double getCouponGoodsPrice(int type, VipListBean.InfoBean.CouponListBean bean) {
        double _money = 0;
        for (int i = 0; i < chooseList.size(); i++) {
            double price = 0;
            GoodBean.DataBean.GoodsBean goods = chooseList.get(i);
            for (int t = 0; t < Arrays.asList(bean.getGoods_id_gather().split(",")).size(); t++) {
                String id = "";
                if (type == 2) {
                    id = chooseList.get(i).getId();
                    Log.e("TAG", "getCouponGoodsPrice:3676 id==============" + id);
                } else {
                    id = chooseList.get(i).getC_id();
                    Log.e("TAG", "getCouponGoodsPrice:3679 id==============" + id);
                }
                if (id.equals(Arrays.asList(bean.getGoods_id_gather().split(",")).get(t))) {
                    if ("2".equals(infoBean.getPlus_type()) && "1".equals(goods.getPlus_mem_status())) { //plus會員
                        if (isSkuGood(i, chooseList)) { //判斷規格商品
                            if (!TextUtils.isEmpty(goods.getSkuList().get(0).getPlus_price())
                                    || (TextUtils.isEmpty(goods.getSkuList().get(0).getPlus_mem_integral()) ||
                                    Integer.parseInt(goods.getSkuList().get(0).getPlus_mem_integral()) == 0)) {
                                price = getVipPrice(1, goods, goods.getSkuList().get(0).getPlus_price());

                            } else {
                                price = getVipPrice(1, goods, goods.getSkuList().get(0).getRetail_price());
                            }
                        } else {
                            if (!TextUtils.isEmpty(goods.getPlus_price())
                                    || (TextUtils.isEmpty(goods.getPlus_mem_integral()) ||
                                    Integer.parseInt(goods.getPlus_mem_integral()) == 0)) {
                                price = getVipPrice(1, goods, goods.getPlus_price());
                            } else {
                                price = getVipPrice(1, goods, goods.getRetail_price());
                            }
                        }
                    } else { //常規會員
                        if ("1".equals(goods.getVip_retail_state())) {
                            if (isSkuGood(i, chooseList)) {
                                if ("2".equals(goods.getSkuList().get(0).getStore_mem_status())) {
                                    price = getVipPrice(1, goods, goods.getRetail_price());
                                } else {
                                    if (!TextUtils.isEmpty(goods.getSkuList().get(0).getStore_mem_price())) {
                                        price = getVipPrice(1, goods, goods.getSkuList().get(0).getStore_mem_price());
                                    } else {
                                        price = getVipPrice(1, goods, goods.getRetail_price());
                                    }
                                }
                            } else {
                                if ("2".equals(goods.getStore_mem_status())) {
                                    price = getVipPrice(1, goods, goods.getRetail_price());
                                } else {
                                    if (!TextUtils.isEmpty(goods.getStore_mem_price())) {
                                        price = getVipPrice(1, goods, goods.getStore_mem_price());
                                    } else {
                                        price = getVipPrice(1, goods, goods.getRetail_price());
                                    }
                                }
                            }
                        } else {
                            if (isSkuGood(i, chooseList)) {
                                if ("2".equals(goods.getSkuList().get(0).getStore_mem_status())) {
                                    price = getVipPrice(1, goods, goods.getRetail_price());
                                } else {
                                    if (!TextUtils.isEmpty(goods.getSkuList().get(0).getStore_mem_price())) {
                                        price = getVipPrice(1, goods, goods.getSkuList().get(0).getStore_mem_price());
                                    } else {
                                        price = getVipPrice(1, goods, goods.getRetail_price());
                                    }
                                }
                            } else {
                                if ("2".equals(goods.getStore_mem_status())) {
                                    price = getVipPrice(1, goods, goods.getRetail_price());
                                } else {
                                    if (!TextUtils.isEmpty(goods.getStore_mem_price())) {
                                        price = Double.parseDouble(goods.getStore_mem_price());
                                    } else {
                                        price = Double.parseDouble(goods.getRetail_price());
                                    }
                                    price = getVipPrice(1, goods, price + "");
                                }
                            }
                        }
                    }
                    _money += MathUtil.mul(price, goods.getAgoods_count());
                }
            }

        }
        return _money;
    }

    /**
     * 獲取會員價格
     */
    private double getVipPrice(int type, GoodBean.DataBean.GoodsBean position, String retail_price) {//type 0:普通用戶 1:會員
        double price = 0;
        //開啓門店折上折
        if ("1".equals(aCache.getAsString("store_manual_discount"))) {
            if (type == 1 && "1".equals(position.getVip_retail_state()) && ("2".equals(position.getPlus_mem_status()) ||
                    "1".equals(position.getPlus_mem_status()) && !"2".equals(infoBean.getPlus_type()))) {
                price = MathUtil.mul(Double.parseDouble(retail_price), infoBean.getDiscount());
            } else {
                price = Double.parseDouble(retail_price);
            }
            price = doubleAndMul(price + "", position.getSku_discount() + "");
        } else {
            if (position.getSku_discount() == 1) {
                if (type == 1 && "1".equals(position.getVip_retail_state()) && ("2".equals(position.getPlus_mem_status()) ||
                        "1".equals(position.getPlus_mem_status()) && !"2".equals(infoBean.getPlus_type()))) {
                    price = MathUtil.mul(Double.parseDouble(retail_price), infoBean.getDiscount());
                } else {
                    price = doubleAndMul(retail_price, position.getSku_discount() + "");
                }
            } else {
                price = doubleAndMul(retail_price, position.getSku_discount() + "");
            }
        }
        if (price < 0.01) {
            price = 0.01;
        }
        return price;
    }

    private boolean isSkuGood(int i, List<GoodBean.DataBean.GoodsBean> chooseList) {
        return "2".equals(chooseList.get(i).getAttribute_state()) &&
                chooseList.get(i).getSkuList() != null &&
                chooseList.get(i).getSkuList().size() > 0;
    }

    /**
     * 獲取兩數相乘的數
     */
    private double doubleAndMul(String retail_price, String vipDiscount) {
        return MathUtil.round(MathUtil.mul(Double.parseDouble(retail_price),
                Double.parseDouble(vipDiscount)), 2);
    }

    private void setChooseCouponAdapter() {
        if (chooseCouponAdapter == null) {
            chooseCouponAdapter = new ChooseCouponAdapter(couponList);
            chooseCouponListView.setAdapter(chooseCouponAdapter);
            select = getIntent().getIntExtra("couponIndex", 9999);
            chooseCouponAdapter.setSelect(select);
        } else {
            chooseCouponAdapter.notifyDataSetChanged();
        }
    }

    @OnClick(R.id.title_back)
    public void onViewClicked() {
        Intent intent = new Intent(this, ChoosePaymentActivity.class);
        intent.putExtra("couponIndex", select);
        setResult(120, intent);
        finish();
    }

    /* 監聽手機系統返回鍵*/
    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, ChoosePaymentActivity.class);
        intent.putExtra("couponIndex", select);
        setResult(120, intent);
        finish();
    }

}
